<?php $this->load->view('templates/backend/header'); ?>
<h3><?php echo $this->lang->line('sales_search');?></h3>
<style>
    .fone_num_cls{
        width:20%;  vertical-align: top;
    }
</style>
<?php if (isset($flash_message)  && $flash_message!=NULL ): ?>
<div class="alert alert-success"><a class="close" sales-dismiss="alert">x</a><strong><?php echo $flash_message; ?></strong></div>
<?php endif; ?>
<div class="well">
    <?php echo form_open('auth/sales/search/',array('id'=>'ajaxform'));?>
        <div class="tab-content">	
            <div class="tab-pane active" id="main">
                <?php 
                 //phone
                echo form_label($this->lang->line('phone'), 'phone');
                echo form_input(array('name' => 'phone', 'id' => 'phone', 'value' => '','placeholder'=>'Nhập SĐT khách hàng vào đây'));
                echo form_error('phone');
                ?>
            </div>
        </div>
        <div class="top-button">
                <?php echo form_button(array('id' => 'submit', 'value' => 'Update', 'name' => 'submit', 'type' => 'submit', 'content' => $this->lang->line('search'),'class' => 'btn btn-success')); ?>
                <?php echo form_button(array('id' => 'edit','content' => $this->lang->line('edit'),'class' => 'btn btn-success')); ?>
        </div>
    <?php echo form_close();?>
    <span id="simple-msg"></span>
</div>

<?php $this->load->view('templates/backend/footer'); ?>

<script>
$(document).ready(function() {
        $("#edit").on('click',function(e) {
                var phone = $("#phone").val();
                if(phone == ""){
                    alert("Bạn chưa nhập SDT! Nhập vào đi rùi Sửa");
                } else {
                    $("#simple-msg").html("<img src='<?php echo base_url().'static/templates/frontend/home/images/';?>loading.gif'/>");
                    setTimeout(function () {
                        $.ajax(
                            {
                                url : '/auth/sales/searchIdByPhone?term='+$("#phone").val(),
                                type: "POST",
                                data : $(this).serializeArray(),
                                cache: true,
                                beforeSend: function(){
                                    
                                },
                                success:function(data) 
                                {
                                    //console.log(data);
                                    if(data == ''){
                                        alert("SĐT này chưa được khởi tạo thông tin!Vui lòng kiểm tra lại");
                                    } else {
                                        window.location.replace('/auth/sales/index/edit/'+data);
                                    }
                                },
                                error: function() 
                                {
                                    $("#simple-msg").html('F5 thử lại!!!!!!');
                                }
                            });
                    }, 222);
                    e.preventDefault();	//STOP default action
                }
        });
       
        $("#ajaxform").submit(function(e)
        {
            $("#simple-msg").html("<img src='<?php echo base_url().'static/templates/frontend/home/images/';?>loading.gif'/>");
            var postData = $(this).serializeArray();
            var formURL = $(this).attr("action");
            setTimeout(function () {
                $.ajax(
                    {
                        url : formURL,
                        type: "POST",
                        data : postData,
                        cache: true,
                        beforeSend: function(){
                            $('button[type="submit"]').attr('readonly');
                        },
                        success:function(data) 
                        {
                            $("#simple-msg").html(data);
                            $('button[type="submit"]').attr('readonly');
                        },
                        error: function() 
                        {
                            $("#simple-msg").html('F5 thử lại!!!!!!');
                        }
                    });
            }, 222);
            e.preventDefault();	//STOP default action
        });
});
</script>